﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Codinfinity.BeIDApplet
{
    public class SessionItem:Dictionary<string,object>
    {
        public DateTime CreationDate { get;private set; }
        public Guid SessionID { get; set; }
        public string IPAddress { get; set; }
        public SessionItem():base()
        {
            CreationDate = DateTime.Now;
            IPAddress = System.Web.HttpContext.Current.Request.UserHostAddress;
        }
        public SessionItem Self { get { return this; } }
        public bool KeyExist(string KeyName)
        {
            return this.Keys.Any(k => k == KeyName);
        }
    }
    public class SessionManager
    {
        public static List<SessionItem> Sessions;
        private static DateTime LastCheck;
        static SessionManager()
        {
            Init();
        }
        public static void Init()
        {
            Sessions = new List<SessionItem>();
            LastCheck = DateTime.Now;
        }

        public static Guid SessionID
        {
            get
            {
                Guid TheSessionID = Guid.NewGuid();

                if (!string.IsNullOrEmpty(System.Web.HttpContext.Current.Request.Params["sid"]))
                {
                    string str = System.Web.HttpContext.Current.Request.Params["sid"].Trim();
                    if (str.IndexOf(",") > -1) str = str.Substring(0, str.IndexOf(","));
                    TheSessionID = new Guid(str);
                }

                else if (System.Web.HttpContext.Current.Request.Cookies["sid"] != null)
                    TheSessionID = new Guid(System.Web.HttpContext.Current.Request.Cookies["sid"].Value);
                /*
            else if (Config.Context.Session["sid"] != null)
                TheSessionID = (Guid)Config.Context.Session["sid"];
           /* else if (Sessions.Any(s => s.IPAddress == System.Web.HttpContext.Current.Request.UserHostAddress))
                TheSessionID = Sessions.First(s => s.IPAddress == System.Web.HttpContext.Current.Request.UserHostAddress).SessionID;
            */
                SessionID = TheSessionID;
                return TheSessionID;
            }
            set
            {
                System.Web.HttpCookie cook = new System.Web.HttpCookie("sid", value.ToString());
                System.Web.HttpContext.Current.Request.Cookies.Add(cook);
                System.Web.HttpContext.Current.Response.Cookies.Add(cook);
                System.Web.HttpContext.Current.Session["sid"] = value;

            }
        }

        public static SessionItem GetClientSession(Guid aSessionID)
        {
            SessionItem retval = new SessionItem();
            bool newsession = true;
            if (Sessions.Any(s => s.SessionID == aSessionID))
            {
                retval = Sessions.First(s => s.SessionID == aSessionID);
                newsession = false;
            }

            if (newsession)
            {
                SessionItem nSession = new SessionItem();
                nSession.SessionID = aSessionID;
                Sessions.Add(nSession);
                retval = nSession;
            }
            return retval;
        }
        public static SessionItem ClientSession
        {
            get
            {
                return GetClientSession(SessionID); 
               
            }
        }
    }
}
